class virtual ['a] list = object(_)
  method virtual hd : 'a
  method virtual tl : 'a list
  method virtual length : int
end

class ['a] cons h t = object(_)
  inherit ['a] list
  method hd = h
  method tl = t
  method length = 1 + t#length
end

class ['a] nil = object(_)
  inherit ['a] list
  method hd = invalid_arg "nil#hd"
  method tl = invalid_arg "nil#tl"
  method length = 0
end
;;

print_int (new cons 7 (new cons 1 (new cons 2 (new nil))))#length;;
print_newline ();;
